package com.weapons.week0903_weapons.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.activity.BbsItemActivity;
import com.weapons.week0903_weapons.bean.BbsBean;

import java.util.List;

/**
 * Created by sun on 2016/7/22.
 */
public class BbsAdapter extends RecyclerView.Adapter<BbsAdapter.BbsHolder> implements View.OnClickListener {
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private Context mContext;
    private List<BbsBean.ItemBean> mDatas;
    private String str = null;

    public BbsAdapter(Context context, List<BbsBean.ItemBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
    }

    @Override
    public BbsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bbs, parent, false);
        BbsHolder holder = new BbsHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(BbsHolder holder, int position) {
        Picasso.with(mContext).load(mDatas.get(position).getImage()).into(holder.iv_bbs_item);
        holder.title_item_bbs.setText(mDatas.get(position).getTitle());
        holder.time_item_bbs.setText(mDatas.get(position).getPubDate());
        holder.num_item_bbs.setText("(" + mDatas.get(position).getComments() + ")评论");
        holder.itemView.setTag(mDatas.get(position).getAid());
    }

    @Override
    public int getItemCount() {
        Log.i("xxx", mDatas.size() + "--------");
        return mDatas == null ? 0 : mDatas.size();
    }

    class BbsHolder extends RecyclerView.ViewHolder {
        private ImageView iv_bbs_item;
        private TextView title_item_bbs;
        private TextView time_item_bbs;
        private TextView num_item_bbs;

        public BbsHolder(View itemView) {
            super(itemView);
            iv_bbs_item = (ImageView) itemView.findViewById(R.id.iv_bbs_item);
            title_item_bbs = (TextView) itemView.findViewById(R.id.title_item_bbs);
            time_item_bbs = (TextView) itemView.findViewById(R.id.time_item_bbs);
            num_item_bbs = (TextView) itemView.findViewById(R.id.num_item_bbs);

        }
    }


    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (String)v.getTag());

        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String data);
    }
}
